using System;
using System.Management.Automation;
using Nivot.PowerShell.WindowsMobile.Rapi2;

namespace Nivot.PowerShell.WindowsMobile
{
    public class WindowsMobileDriveInfo : PSDriveInfo
    {
        private readonly WindowsMobileObjectModel m_objectModel;

        internal WindowsMobileDriveInfo(string name, WindowsMobileProviderInfo providerInfo, string root,
                                        string description, PSCredential credential)
            : base(name, providerInfo, root, description, credential)
        {
            RemoteDevice device = providerInfo.DeviceManager.Devices.FirstConnectedDevice;
            m_objectModel = new WindowsMobileObjectModel(device, root);

            m_objectModel.IsConnected = (device.Status == DeviceStatus.Connected);

            providerInfo.DeviceManager.DeviceConnected += new EventHandler(DeviceManager_DeviceConnected);
            providerInfo.DeviceManager.DeviceDisconnected += new EventHandler(DeviceManager_DeviceDisconnected);
        }

        internal WindowsMobileObjectModel ObjectModel
        {
            get { return m_objectModel; }
        }

        private void DeviceManager_DeviceDisconnected(object sender, EventArgs e)
        {
            m_objectModel.IsConnected = false;
        }

        private void DeviceManager_DeviceConnected(object sender, EventArgs e)
        {
            m_objectModel.IsConnected = true;
        }
    }
}